
//baseURL
axios.defaults.baseURL='http://pcapi-xiaotuxian-front-devtest.itheima.net'


const elem=document.querySelector('main')
var type=window.localStorage.getItem('logon')
var cartItems=window.localStorage.getItem('cartItems')




// 界面渲染
if (type==='true'){
   if(!cartItems){
      elem.innerHTML=`
      <div class="iot_cartlogo">
      <img src="../images/cartlogo.png" alt="">
    </div>
      `
   }
   else{
      var cartItems=JSON.parse(localStorage.getItem('cartItems'))
      
      //carts
     function Createcart(select,data){
      this.elem=document.querySelector(select);
      this.data=data
     }
     //原型
     Createcart.prototype.render= function(){
                  //渲染页面（this指向：谁调用指向谁）
                  
                  this.data.forEach(elem=>{
                      this.elem.innerHTML = this.data.map(item => `<div>
                      <div class="iot_good_input">
                       <input type="checkbox" checked>
                      </div>
                      <div class="iot_good_img"><img src="${item.img}" alt=""></div>
                      <div class="iot_good_text">
                       <p>${item.good.name}</p>
                       <span>￥${item.price}</span>
                      </div>
                      </div>`).join('');
                   })
                  
            }
  
               var cart_obj=new Createcart('main',cartItems);
               cart_obj.render();
   }
   
}
else{
   elem.innerHTML=`
   <div class="iot_logonbefore">
      <div class="iot_text">
        <p>登录后可查看购物车中的商品</p>
        <div><button><a href="../html/登录.html">去登录</a></button></div>
     </div>
    </div>`
}



//猜你喜欢部分

function guess_like_axios(){
   var config={
        method:'get',
        url:'/home/goods/guessLike',
        headers: { 
         'User-Agent': 'Apifox/1.0.0 (https://apifox.com)'
      }
   }

   axios(config)
   .then(res=>{
     var data=res.data.result.items
     //猜你喜欢
     class CreateLike{
      //构造器函数(属性挂载)
      constructor(select,data){
         this.elem=document.querySelector(select);
         this.data=data
      }
      
      //方法
      render(){
         this.data.forEach(elem=>{
            this.elem.innerHTML = this.data.map(item => `<div>
            <div><a href='../html/商品详细.html?url=/goods/?id=${item.id}' axios_url=/goods/?id=${item.id}><img src=${item.picture} alt=""><a/></div>
            <p>${item.desc}</p>
            <span>￥${item.price}</span>
           </div>`).join('');
         })
      }
   }
  
               var obj=new CreateLike('.iot_guess_like',data);
               obj.render();
   
     
   })
}
guess_like_axios()



// 全选
var check_all=document.querySelector('.iot_pay_checkbox>input')
var checks=document.querySelectorAll('.iot_good_input>input')

check_all.addEventListener('click',function(){
   checks.forEach(e=>{
      var type=check_all.checked
      e.checked = type
   })
})